package com.kang.dead;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhangkangkang
 * @Description: 死信队列
 * @date 2021/8/3 14:13
 */
public class Consumer {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();

        Map map = new HashMap();
        map.put("x-max-length", 10);
        map.put("x-dead-letter-exchange", "deadexchange");
        map.put("x-dead-letter-routing-key", "routingkey");
        map.put("x-overflow", "reject-publish");

        channel.queueDeclare("mq1", false, false, false, map);
        channel.basicConsume("mq1", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
